package route

import (
	"forum/app/controller/admin/cate"
	"forum/app/controller/admin/user"
	"net/http"
)

func WithCORS(handler http.HandlerFunc) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		w.Header().Set("Access-Control-Allow-Origin", "*")
		w.Header().Set("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS")
		w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Authorization")

		if r.Method == "OPTIONS" {
			w.WriteHeader(http.StatusOK)
			return
		}

		handler(w, r)
	}
}
func InitAdmin() {
	http.HandleFunc("/admin/user/login", WithCORS(user.Login))
	http.HandleFunc("/admin/user/getUserInfo", WithCORS(user.GetUserInfo))

	http.HandleFunc("/admin/cate/getList", WithCORS(cate.GetList))
	http.HandleFunc("/admin/cate/create", WithCORS(cate.Create))
}
